﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using System.Data.SqlClient;
namespace WebApp
{
    public class SqlHelper
    {
        private static string strCn = "server=pc.hstc.cn;database=dhd2014112102;uid=2014112102;pwd=19960523;";

        public static SqlDataReader ExecuteReader(string strSql, params SqlParameter[] pms)
        {
            SqlDataReader dr = null;
            SqlConnection cn = new SqlConnection(strCn);
            cn.Open();
            SqlCommand cmd = new SqlCommand(strSql, cn);
            if (pms != null) cmd.Parameters.AddRange(pms);
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return dr;
        }

        public static void ExecuteScalar(string strSql, params SqlParameter[] pms)
        {
            object obj = null;
            using (SqlConnection cn = new SqlConnection(strCn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(strSql, cn))
                {
                    if (pms != null) cmd.Parameters.AddRange(pms);
                    obj = cmd.ExecuteScalar();
                }
            }
        }
        public static int ExecuteNonQuery(string strSql, params SqlParameter[] pms)
        {
            int iCount = 0;
            using (SqlConnection cn = new SqlConnection(strCn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(strSql, cn))
                {
                    if (pms != null) cmd.Parameters.AddRange(pms);
                    iCount = cmd.ExecuteNonQuery();
                }
            }
            return iCount;
        }
    }
}